package com.itheima.stock.service.impl;

import com.itheima.stock.pojo.SysPermission;
import com.itheima.stock.service.PermissionService;
import com.itheima.stock.vo.resp.PermissionRespNodeVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;
@Service
public class PermissionServiceImpl implements PermissionService {

    @Override
    public List<PermissionRespNodeVo> getTree(List<SysPermission> permissions, String pid, boolean isOnlyMenuType) {
        //构建权限树集合
        if (CollectionUtils.isEmpty(permissions)) {
            return null;
        }
        //组装权限树
        List<PermissionRespNodeVo> infos = permissions.stream()
                .filter(p -> p.getPid().equals(pid) && (p.getType().intValue() != 3 || !isOnlyMenuType))
                .map(p -> {
                    PermissionRespNodeVo prnv = PermissionRespNodeVo.builder().id(p.getId()).title(p.getTitle())
                            .icon(p.getIcon()).path(p.getUrl()).name(p.getName())
                            .children(getTree(permissions, p.getId(), isOnlyMenuType))
                            .build();
                    return prnv;
                }).collect(Collectors.toList());
        return infos;
    }
}
